package cn.iocoder.boot.mqtt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.stereotype.Service;

@Service
public class MqttDynamicSubscriptionService {

    @Autowired
    private MqttPahoMessageDrivenChannelAdapter adapter;

    /**
     * 订阅指定主题
     *
     * 该方法通过添加一个新的主题到适配器来实现订阅主题的操作这使得类可以接收和处理与该主题相关的消息或数据
     *
     * @param topic 要订阅的主题名称
     */
    public void subscribeToTopic(String topic) {
        adapter.addTopic(topic);
    }

    public void unsubscribeFromTopic(String topic) {
        adapter.removeTopic(topic);
    }
}